package com.yanchangapp.ui.activity.manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yanchangapp.R;
import com.yanchangapp.adapter.MgPlanListAdapter;
import com.yanchangapp.model.bean.MgPlanListBean;
import com.yanchangapp.ui.presenter.MgDailyListAtPresenter;
import com.yanchangapp.ui.view.MgDailyListAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的计划  -- 今日计划列表页面
 */
public class MgDailyListActivity extends BaseActivity<MgDailyListAtView, MgDailyListAtPresenter> implements MgDailyListAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tv_publish_now)
    TextView tvPublishNow;
    @BindView(R.id.rvMgList)
    RecyclerView rvMgList;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.llEmptyMail)
    LinearLayout llEmptyMail;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    // 检测当前是否有数据进行显示了...
    private List<MgPlanListBean> mMgPlanListBeans;
    private MgPlanListAdapter mMgPlanListAdapter;
    @Override
    protected void init() {
        mMgPlanListBeans = new ArrayList<>();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_mg_daily_list;
    }

    @Override
    protected MgDailyListAtPresenter createPresenter() {
        return new MgDailyListAtPresenter(this);
    }

    public static void show(Activity activity){
        activity.startActivity(new Intent(activity,MgDailyListActivity.class));
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvPublishNow.setVisibility(View.VISIBLE);
        tvPublishNow.setText("添加计划");
        tvToolbarTitle.setText("今日计划");
        // 禁止下拉加载更多
        refreshLayout.setEnableLoadMore(false);
        refreshList();
        initAdapter();
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        rvMgList.setLayoutManager(linearLayoutManager);

        mMgPlanListAdapter = new MgPlanListAdapter(R.layout.item_mg_plan_list,mMgPlanListBeans);
        rvMgList.setAdapter(mMgPlanListAdapter);
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initListener() {
        // 返回按钮点击事件
        ivToolbarNavigation.setOnClickListener(v -> {

        });

        // 添加计划点击事件
        tvPublishNow.setOnClickListener(v -> {
            // 跳转向添加计划界面
            jumpToActivity(MgAddPlanActivity.class);
        });

        // 列表Item点击事件
        mMgPlanListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }
        });

        // 列表子组建点击事件
        mMgPlanListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }
        });
    }

    // 下拉刷新回调处理
    private void refreshList(){
        // 设置刷新now...
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                UIUtils.postTaskDelay(() -> doRefresh(), 1000);
            }
        });
    }

    // 进行刷新
    private void doRefresh(){
        mPresenter.queryPlanLists();
    }

    private void stateContent(){
        refreshLayout.setEnableRefresh(true);
        if (llEmptyMail.getVisibility() == View.VISIBLE){
            llEmptyMail.setVisibility(View.GONE);
        }

        if (refreshLayout.getVisibility() == View.GONE){
            refreshLayout.setVisibility(View.VISIBLE);
        }

        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }
    }

    private void stateError(){
        refreshLayout.setEnableRefresh(false);
        if (llEmptyMail.getVisibility() == View.VISIBLE){
            llEmptyMail.setVisibility(View.GONE);
        }

        if (refreshLayout.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.GONE){
            llErrorNetwork.setVisibility(View.VISIBLE);
        }
    }

    // 设置选中的状态
    private void stateEmpty(){
        refreshLayout.setEnableRefresh(false);
        if (llEmptyMail.getVisibility() == View.GONE){
            llEmptyMail.setVisibility(View.VISIBLE);
        }

        if (refreshLayout.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            refreshLayout.setVisibility(View.GONE);
        }
    }

    // 判断列表是否有内容
    private boolean hasContent(){
        return  mMgPlanListBeans.size() != 0;
    }

    @Override
    public void queryMailListsSuccess(List<MgPlanListBean> mgPlanListBeans) {
        // 设置为完成刷新
        if (refreshLayout.getState() == RefreshState.Refreshing){
            refreshLayout.finishRefresh();
        }
        mMgPlanListBeans.clear();
        if (mgPlanListBeans != null){
            mMgPlanListBeans.addAll(mgPlanListBeans);
        }

        if (!hasContent()){
            stateEmpty();
        }else {
            stateContent();
            mMgPlanListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void queryMailListsFailure(String err) {
        // 设置为完成刷新
        if (refreshLayout.getState() == RefreshState.Refreshing){
            refreshLayout.finishRefresh();
        }

        if (!TextUtils.isEmpty(err)){
            UIUtils.showToast(err);
        }

        if (!hasContent()){
            stateError();
        }
    }
}
